<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>冒泡排序法</title>
</head>
<body>
    <p>通过相邻两元素的不断比较 --- b站毕向东30天java</p>
    <script>
        var arr = [2,9,1,8,3,7,4,5,6]
        function maoPao(arr) {
            var temp ;
            for (let i = 0; i < arr.length - 1; i++) {
                for (let j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j]<arr[j+1]) {
                        temp = arr[j+1];
                        arr[j+1] = arr[j];
                        arr[j] = temp; 
                    }
                }
            }
        }
        function maoPao2(arr) {
            var temp ;
            for (let i = arr.length - 1; i > 0; i--) {
                for (let j = 0; j < i; j++) {
                    if (arr[j]<arr[j+1]) {
                        temp = arr[j+1];
                        arr[j+1] = arr[j];
                        arr[j] = temp; 
                    }
                }
            }
        }
        console.log(arr);
        maoPao(arr);
        // maoPao2(arr);
        console.log(arr);
    </script>
</body>
</html>